import type { AxiosResponse } from '@vben/request';

import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    username: string;
    password: string;
    code: string;
    uuid: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    token: string;
    msg: string;
    code: number;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }

  export interface RegisterCodeResult {
    msg: string;
    img: string;
    code: number;
    captchaOnoff: boolean;
    uuid: string;
  }

  /** 注册接口参数 */
  export interface RegisterParams {
    username: string;
    password: string;
    code: string;
    uuid: string;
  }

  /** 注册接口返回值 */
  export interface RegisterResult {
    code: number;
    msg: string;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/login', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}

/**
 * 获取验证码
 */
export async function getCode() {
  // return requestClient.get<AuthApi.RegisterCodeResult>('/captchaImage');
  return baseRequestClient.get<AxiosResponse<AuthApi.RegisterCodeResult>>(
    '/captchaImage',
  );
}

/**
 * 注册
 */
export async function register(data: AuthApi.RegisterParams) {
  // return requestClient.post('/register', data);
  return baseRequestClient.post<AxiosResponse<AuthApi.RegisterResult>>(
    '/register',
    data,
  );
}
